﻿[assembly: System.Web.UI.TagPrefix("GSC.WebControls", "GSC")]
namespace GSC.WebControls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.Design.WebControls;
    using System.Drawing.Design;
    using System.Drawing;
    using System.Web.UI.Design;
    using System.ComponentModel;
    using ComponentModel.Design;
    using System.Security.Permissions;
    using System.Collections.Generic;
    /// <summary>
    /// 左边垂直菜单控件。
    /// </summary>
    /// <remarks>左边垂直菜单控件。</remarks>
    [ToolboxItemFilter("WebControls", ToolboxItemFilterType.Custom)]
    [ToolboxData("<{0}:BackgroundCenterLeftMenuControl runat=server width=150px></{0}:BackgroundCenterLeftMenuControl>")]
    [SRDescription("Manage_CenterLeftMenu", typeof(Properties.Resources))]
    [Designer(typeof(BackgroundCenterLeftMenuControlDesigner))]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    //[PersistChildren(false)]
    //[DefaultProperty("Titles")]
    //[ParseChildren(true, "Titles")]
    //[ToolboxBitmap(typeof(BackgroundCenterLeftMenuControl), "BackgroundCenterLeftMenuControl.bmp")]  
    public class BackgroundCenterLeftMenuControl : DataBoundControl, INamingContainer, IPostBackEventHandler, ICompositeControlDesignerAccessor, ICenterLeftMenuTitleCollection, IAsyncControlIDsExtender
    {
        #region IAsyncControlIDsExtender
        /// <summary>
        /// 
        /// </summary>
        List<string> IAsyncControlIDsExtender.ControlIDs
        {
            get
            {
                List<string> ids = new List<string>();
                //if (this.AutomaticLoadControl)
                //{
                    foreach (BackgroundCenterLeftMenuTitleControl item in this.Titles)
                    {
                        ids.Add(item.Button.ClientID);
                        foreach (BackgroundCenterLeftMenuButtonControl citem in item.Buttons)
                        {
                            //string asxc = citem.Ascx;
                            //if (asxc != string.Empty && asxc.EndsWith(".ascx", StringComparison.InvariantCultureIgnoreCase))
                            //{
                                ids.Add(citem.Button.ClientID);
                            //}
                        }
                    }
                //}
                return ids;
            }
        }
        #endregion
        #region ICompositeControlDesignerAccessor
        void ICompositeControlDesignerAccessor.RecreateChildControls()
        {
            base.ChildControlsCreated = false;
            EnsureChildControls();
        }
        #endregion
        /*
        #region IPostBackDataHandler
        /// <summary>
        /// 
        /// </summary>
        /// <param name="postDataKey"></param>
        /// <param name="postCollection"></param>
        /// <returns></returns>
        bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
        {

            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        void IPostBackDataHandler.RaisePostDataChangedEvent()
        {
            this.OnChanged(EventArgs.Empty);
        }
        #region Changed
        /// <summary>
        /// 客户端提交操作更改事件。
        /// </summary>
        /// <remarks>客户端提交操作更改事件。</remarks>
        [Browsable(true)]
        public event EventHandler Changed
        {
            add
            {
                Events.AddHandler(_changedObject, value);
            }
            remove
            {
                Events.RemoveHandler(_changedObject, value);
            }
        }
        private static readonly object _changedObject = new object();
        /// <summary>
        /// 客户端提交操作更改事件。
        /// </summary>
        /// <remarks>客户端提交操作更改事件。</remarks>
        /// <param name="e"></param>
        protected virtual void OnChanged(EventArgs e)
        {
            EventHandler handler = (EventHandler)Events[_changedObject];
            if (handler != null)
            {
                handler(this, e);
            }
        }
        #endregion
        #endregion
         */ 
        #region IPostBackEventHandler
        /// <summary>
        /// 客户端pos事件处理
        /// </summary>
        /// <param name="eventArgument"></param>
        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            //转换客户端递交事件内容
            string[] Argument = eventArgument.Split(new Char[] { '&' });
            int n = 0;
            int cn = Convert.ToInt32(Argument[1]);
            //事件分类执行
            switch (Argument[0])
            {
                case "Title":
                    BackgroundCenterLeftMenuTitleControl title = null;
                    bool isExpanded = false;
                    //展开操作。
                    if (this.MultiLine)
                    {
                        foreach (BackgroundCenterLeftMenuTitleControl item in this.Titles)
                        {
                            if (cn == n)
                            {
                                isExpanded = item.IsExpanded;
                                title = item;
                                if (item.IsExpanded)
                                    item.IsExpanded = false;
                                else
                                    item.IsExpanded = true;
                                break;
                            }
                            n++;
                        }
                    }
                    else
                    {
                        foreach (BackgroundCenterLeftMenuTitleControl item in this.Titles)
                        {
                            if (cn == n)
                            {
                                isExpanded = item.IsExpanded;
                                title = item;
                                if (item.IsExpanded)
                                    item.IsExpanded = false;
                                else
                                    item.IsExpanded = true;
                            }
                            else
                            {
                                if (item.IsExpanded)
                                {
                                    item.IsExpanded = false;
                                }
                            }
                            n++;
                        }
                    }
                    this.OnAction(new BackgroundCenterLeftMenuControlActionEventArgs(title, isExpanded ? false : true, isExpanded ? true : false, title.CommandName, title.CommandArgument));
                    break;
                case "Button":
                    BackgroundCenterLeftMenuButtonControl button = null;
                    foreach (BackgroundCenterLeftMenuTitleControl item in this.Titles)
                    {
                        foreach (BackgroundCenterLeftMenuButtonControl citem in item.Buttons)
                        {
                            if (cn == n)
                            {
                                button = citem;
                                if (!button.IsSelected)
                                {
                                    button.IsSelected = true;
                                }
                            }
                            else
                            {
                                if (citem.IsSelected)
                                {
                                    citem.IsSelected = false;
                                }
                            }
                            n++;
                        }
                    }
                    //加载控件。
                    /*
                    if (button is IAutomaticLoadControl)
                    {
                        IAutomaticLoadControl item = (IAutomaticLoadControl)button;
                        if (this.AutomaticLoadControlID != string.Empty && this.AutomaticLoadControl && item.Ascx != string.Empty && item.Ascx.EndsWith(".ascx", StringComparison.InvariantCultureIgnoreCase))
                        {
                            Control control = this.Parent.FindControl(this.AutomaticLoadControlID);
                            if (control is IUpdateAscx)
                            {
                                ((IUpdateAscx)control).UpdateAscx(item.Ascx);
                            }
                        }
                    }
                     */
                    //
                    BackgroundControl master = null;
                    if (this.AutomaticLoadControlID != string.Empty)
                    {
                        master = this.Parent.FindControl(this.AutomaticLoadControlID) as BackgroundControl;
                    }
                    //
                    if (master == null)
                    {
                        master = BackgroundControl.GetCurrent(this.Page);
                    }
                    //
                    if (master != null)
                    {
                        if (button.IsDefaultMenu)
                        {
                            master.DefaultUpdateMenuAscx();
                        }
                        else if (button.IsMenuBack)
                        {
                            master.LoadParentMenu();
                        }
                        else
                        {
                            this.AutomaticLoadControl = master.AutomaticLoadControl;
                            //
                            if (this.AutomaticLoadControl && button.Ascx != string.Empty && button.Ascx.EndsWith(".ascx", StringComparison.InvariantCultureIgnoreCase))
                            {
                                //验证加载的是否为菜单子控件。
                                if (button.IsMenu)
                                    //加载菜单控件。
                                    ((IUpdateMenu)master).LoadChildMenu(button.Ascx);
                                else
                                    //加载内容控件。
                                    master.UpdateAscx(button.Ascx);
                            }
                        }
                    }
                    this.OnCommand(new BackgroundCenterLeftMenuControlCommandEventArgs(button, button.CommandName, button.CommandArgument));
                    break;
            }
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            Page.RegisterRequiresControlState(this);
            base.OnInit(e);
            if (this.AutomaticLoadControlID != string.Empty && this.AutomaticLoadControl)
            {
                Control control = this.Parent.FindControl(this.AutomaticLoadControlID);
                if (control is IUpdateAscx)
                {
                    ((IUpdateAscx)control).UpdateAscx();
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }
        /// <summary>
        /// 设计时获取资源文件地址。
        /// </summary>
        /// <remarks>设计时获取资源文件地址。</remarks>
        /// <returns>string</returns>
        internal virtual string GetWebResourceUrl(string resourceUrl)
        {
            if (this.Page == null) return string.Empty;
            if (this.Page.ClientScript == null) return string.Empty;
            return this.Page.ClientScript.GetWebResourceUrl(typeof(BackgroundCenterLeftMenuControl), "GSC.WebControls.BackgroundCenterLeftMenuControl.Images." + resourceUrl);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        protected override void AddParsedSubObject(object obj)
        {
            if (obj is BackgroundCenterLeftMenuTitleControl)
                this.Titles.Add((BackgroundCenterLeftMenuTitleControl)obj);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected override string TagName
        {
            get
            {
                return "div";
            }
        }
        #region IStateManager
        /// <summary>
        /// 
        /// </summary>
        /// <param name="savedState"></param>
        protected override void LoadViewState(object savedState)
        {
            Triplet t = savedState as Triplet;
            if (t != null)
            {
                if (t.First != null)
                {
                    base.LoadViewState(t.First);
                }
                if (t.Second != null)
                {
                    ((IStateManager)this.Titles).LoadViewState(t.Second);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override object SaveViewState()
        {
            Triplet t = new Triplet();
            t.First = base.SaveViewState();
            t.Second = ((IStateManager)this.Titles).SaveViewState();
            
            return t;
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void TrackViewState()
        {
            base.TrackViewState();
            ((IStateManager)this.Titles).TrackViewState();
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Clear();
            CreateControlHierarchy();
            ClearChildViewState();
        }
        private BackgroundCenterLeftMenuTitleControlCollection _titles = null;
        /// <summary>
        /// 获取标题集合。
        /// </summary>
        /// <remarks>获取标题集合。</remarks>
        /// <value>WebControls.LeftMenuTitleCollection</value>
        [
        PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        EditorAttribute(typeof(BackgroundCenterLeftMenuTitleControlCollectionEditor), typeof(UITypeEditor)),
        NotifyParentProperty(true)
        ]
        [SRDescription("Manage_CenterLeftMenu_Titles", typeof(Properties.Resources))]
        public BackgroundCenterLeftMenuTitleControlCollection Titles
        {
            get
            {
                if (_titles == null)
                {
                    _titles = new BackgroundCenterLeftMenuTitleControlCollection();
                    if (IsTrackingViewState)
                    {
                        ((IStateManager)_titles).TrackViewState();
                    }
                }
                return _titles;
            }
        }

        /// <summary>
        /// 获取或设置标题高度。
        /// </summary>
        /// <remarks>获取或设置标题高度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "29px")]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Layout")]
        [SRDescription("Manage_CenterLeftMenu_TitleHeight", typeof(Properties.Resources))]
        [TypeConverter(typeof(UnitConverter))]
        public virtual Unit TitleHeight
        {
            get
            {
                object o = this.ViewState["TitleHeight"];
                if (o == null)
                {
                    return Unit.Pixel(29);
                }
                else
                {
                    return (Unit)o;
                }
            }
            set
            {
                this.ViewState["TitleHeight"] = value;
            }
        }
        /// <summary>
        /// 获取或设置按钮高度。
        /// </summary>
        /// <remarks>获取或设置按钮高度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "26px")]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Layout")]
        [TypeConverter(typeof(UnitConverter))]
        [SRDescription("Manage_CenterLeftMenu_ButtonHeight", typeof(Properties.Resources))]
        public virtual Unit ButtonHeight
        {
            get
            {
                object o = this.ViewState["ButtonHeight"];
                if (o == null)
                {
                    return Unit.Pixel(26);
                }
                else
                {
                    return (Unit)o;
                }
            }
            set
            {
                this.ViewState["ButtonHeight"] = value;
            }
        }
        /// <summary>
        /// 获取或设置标题背景图片。
        /// </summary>
        /// <remarks>获取或设置标题背景图片。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Appearance")]
        [SRDescription("Manage_CenterLeftMenu_TitleBackImage", typeof(Properties.Resources))]
        public string TitleBackImage
        {
            get
            {
                object o = this.ViewState["TitleBackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["TitleBackImage"] = value;
            }
        }
        /// <summary>
        /// 获取或设置折叠图标。
        /// </summary>
        /// <remarks>获取或设置折叠图标。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Appearance")]
        [SRDescription("CollapseIcon", typeof(Properties.Resources))]
        public string CollapseIcon
        {
            get
            {
                object o = this.ViewState["CollapseIcon"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["CollapseIcon"] = value;
            }
        }
        /// <summary>
        /// 获取或设置展开图标。
        /// </summary>
        /// <remarks>获取或设置展开图标。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Appearance")]
        [SRDescription("ExpandedIcon", typeof(Properties.Resources))]
        public string ExpandedIcon
        {
            get
            {
                object o = this.ViewState["ExpandedIcon"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["ExpandedIcon"] = value;
            }
        }
        /// <summary>
        /// 获取或设置是否可以多展开标题。
        /// </summary>
        /// <remarks>获取或设置是否可以多展开标题。</remarks>
        /// <value>bool</value>
        [DefaultValue(false)]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Behavior")]
        [SRDescription("Manage_CenterLeftMenu_MultiLine", typeof(Properties.Resources))]
        public bool MultiLine
        {
            get
            {
                object o = this.ViewState["MultiLine"];
                if (o == null) return false;
                return (bool)o;
            }
            set
            {
                if (!value)
                {
                    bool ise = true;
                    foreach (BackgroundCenterLeftMenuTitleControl item in this.Titles)
                    {
                        if (ise && item.IsExpanded)
                        {
                            ise = false;
                        }
                        else
                        {
                            item.IsExpanded = false;
                        }
                    }
                }
                this.ViewState["MultiLine"] = value;
            }
        }
        /// <summary>
        /// 获取或设置按钮背景图片。
        /// </summary>
        /// <remarks>获取或设置按钮背景图片。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Appearance")]
        [SRDescription("Manage_CenterLeftMenu_ButtonBackImage", typeof(Properties.Resources))]
        public string ButtonBackImage
        {
            get
            {
                object o = this.ViewState["ButtonBackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["ButtonBackImage"] = value;
            }
        }
        /// <summary>
        /// 获取或设置选中按钮背景图片。
        /// </summary>
        /// <remarks>获取或设置选中按钮背景图片。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Appearance")]
        [SRDescription("Manage_CenterLeftMenu_SelectedButtonBackImage", typeof(Properties.Resources))]
        public string SelectedButtonBackImage
        {
            get
            {
                object o = this.ViewState["SelectedButtonBackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["SelectedButtonBackImage"] = value;
            }
        }

        /// <summary>
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                this.EnsureChildControls();
            }
            else
            {
                this.Style["font-size"] = "0px";
                this.Style["height"] = "auto";
                this.Style["overflow"] = "hidden";
                int b = 0;
                int a = 0;
                foreach (BackgroundCenterLeftMenuTitleControl item in this.Titles)
                {
                    //
                    item.Button.Attributes["href"] = "javascript:__doPostBack('" + this.UniqueID + "','Title&" + a.ToString() + "')";
                    Page.ClientScript.RegisterForEventValidation(this.UniqueID, "Title&" + a.ToString());
                    a++;
                    //item.Button.CausesValidation = true;
                    item.Button.Text = item.Text;
                    item.Button.ForeColor = item.ForeColor;
                    item.Button.Font.CopyFrom(item.Font);
                    item.Button.CommandName = item.CommandName;
                    item.Button.CommandArgument = item.CommandArgument;
                    //
                    item.Style["overflow"] = "hidden";
                    item.Width = this.Width;
                    //
                    item.TitleContainer.Style["background-position"] = "left top";
                    item.TitleContainer.Width = this.Width;
                    item.TitleContainer.Height = this.TitleHeight;
                    if (string.IsNullOrEmpty(this.TitleBackImage))
                        item.TitleContainer.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("TitleBg.gif")) + ")";
                    else
                        item.TitleContainer.Style["background-image"] = "url(" + this.ResolveClientUrl(this.TitleBackImage) + ")";
                    //
                    item.IconContainer.Style["float"] = "left";
                    item.IconContainer.Style["padding-top"] = "5px";
                    item.IconContainer.Style["padding-left"] = "1px";
                    item.IconContainer.Style["font-size"] = "0px";
                    item.IconContainer.Style["overflow"] = "hidden";
                    //
                    if (item.IsExpanded)
                    {
                        if (this.ExpandedIcon == string.Empty)
                            item.Icon.ImageUrl = this.ResolveClientUrl(this.GetWebResourceUrl("Expanded.gif"));
                        else
                            item.Icon.ImageUrl = this.ResolveClientUrl(this.ExpandedIcon);
                    }
                    else
                    {
                        if (this.CollapseIcon == string.Empty)
                            item.Icon.ImageUrl = this.ResolveClientUrl(this.GetWebResourceUrl("Collapse.gif"));
                        else
                            item.Icon.ImageUrl = this.ResolveClientUrl(this.CollapseIcon);
                        //
                        item.ContentContainer.Style["display"] = "none";
                    }
                    //
                    item.ButtonContainer.Style["float"] = "left";
                    item.ButtonContainer.Style["padding-top"] = "5px";
                    item.ButtonContainer.Style["padding-left"] = "0px";
                    item.ButtonContainer.Style["font-size"] = "0px";
                    item.ButtonContainer.Style["overflow"] = "hidden";
                    //
                    if (!this.TitleTextWidth.IsEmpty)
                    {
                        item.Button.ToolTip = item.Text;
                        item.ButtonContainer.Style["white-space"] = "nowrap";
                        item.ButtonContainer.Style["text-overflow"] = "ellipsis";
                        item.ButtonContainer.Style["-o-text-overflow"] = "ellipsis";
                        item.ButtonContainer.Style["width"] = this.TitleTextWidth.ToString();
                    }
                    int titleTextMarginTop = (int)this.TitleTextMarginTop.Value;
                    if (this.Page.Request.Browser.IsBrowser("IE"))
                    {
                        if (this.Page.Request.Browser.MajorVersion == 8)
                        {
                            item.ButtonContainer.Style["padding-top"] = (titleTextMarginTop + 3).ToString() + "px";
                        }
                        else if (this.Page.Request.Browser.MajorVersion == 7)
                        {
                            item.ButtonContainer.Style["padding-top"] = (titleTextMarginTop + 1).ToString() + "px";
                        }
                        else
                        {
                            item.ButtonContainer.Style["padding-top"] = titleTextMarginTop.ToString() + "px";
                        }
                    }
                    else if (this.Page.Request.Browser.IsBrowser("Opera"))
                    {
                        item.ButtonContainer.Style["padding-top"] = (titleTextMarginTop + 3).ToString() + "px";
                    }
                    else if (this.Page.Request.Browser.IsBrowser("Safari"))
                    {
                        item.ButtonContainer.Style["padding-top"] = (titleTextMarginTop + 1).ToString() + "px";
                    }
                    else
                    {
                        item.ButtonContainer.Style["padding-top"] = titleTextMarginTop.ToString() + "px";
                    }
                    //
                    item.Button.Style["font-size"] = "14px";
                    item.Button.Style["font-weight"] = "bold";
                    //
                    item.ContentContainer.Width = item.Width;
                    if (item.Buttons.Count == 0)
                    {
                        item.ContentContainer.Height = Unit.Pixel(0);
                        item.ContentContainer.Style["display"] = "none";
                    }
                    //
                    foreach (BackgroundCenterLeftMenuButtonControl citem in item.Buttons)
                    {
                        citem.Button.Attributes["href"] = "javascript:__doPostBack('" + this.UniqueID + "','Button&" + b.ToString() + "')";
                        Page.ClientScript.RegisterForEventValidation(this.UniqueID, "Button&" + b.ToString());
                        b++;
                        //
                        citem.Style["overflow"] = "hidden";
                        citem.Style["background-position"] = "left top";
                        citem.Style["white-space"] = "nowrap";
                        citem.Style["text-overflow"] = "ellipsis";
                        citem.Height = this.ButtonHeight;
                        citem.Width = this.Width;
                        if (citem.IsSelected)
                            if (string.IsNullOrEmpty(this.SelectedButtonBackImage))
                                citem.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("SelectedButtonBg.gif")) + ")";
                            else
                                citem.Style["background-image"] = "url(" + this.ResolveClientUrl(this.SelectedButtonBackImage) + ")";
                        else
                            if (string.IsNullOrEmpty(this.ButtonBackImage))
                                citem.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("ButtonBg.gif")) + ")";
                            else
                                citem.Style["background-image"] = "url(" + this.ResolveClientUrl(this.ButtonBackImage) + ")";
                        //
                        citem.ButtonContainer.Height = this.ButtonHeight;
                        citem.ButtonContainer.Width = this.Width;
                        citem.ButtonContainer.Style["overflow"] = "hidden";
                        citem.ButtonContainer.Style["font-size"] = "0px";
                        citem.ButtonContainer.Style["padding-top"] = "5px";
                        citem.ButtonContainer.Style["padding-left"] = "4px";
                        //
                        citem.Button.Style["font-size"] = "12px";

                    }
                }
                //
                if (this.ShowEnd)
                {
                    this._end.Visible = true;
                    this._end.Style["background-position"] = "left top";
                    this._end.Width = this.Width;
                    this._end.Height = this.EndHeight;
                    if (string.IsNullOrEmpty(this.TitleBackImage))
                        this._end.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("EndBg.gif")) + ")";
                    else
                        this._end.Style["background-image"] = "url(" + this.ResolveClientUrl(this.TitleBackImage) + ")";
                }
                else
                {
                    this._end.Visible = false;
                }
            }
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);
            base.Render(writer);
        }
        #region MenuCommand
        private static readonly object _commandObject = new object();
        /// <summary>
        /// 按钮点击事件。
        /// </summary>
        /// <remarks>按钮点击事件。</remarks>
        [Browsable(true)]
        public event CenterLeftMenuCommandEventHandler Command
        {
            add
            {
                Events.AddHandler(_commandObject, value);
            }
            remove
            {
                Events.RemoveHandler(_commandObject, value);
            }
        }
        /// <summary>
        /// 按钮点击事件。
        /// </summary>
        /// <remarks>按钮点击事件。</remarks>
        /// <param name="e"></param>
        protected virtual void OnCommand(BackgroundCenterLeftMenuControlCommandEventArgs e)
        {
            CenterLeftMenuCommandEventHandler handler = (CenterLeftMenuCommandEventHandler)Events[_commandObject];
            if (handler != null)
            {
                handler(this, e);
            }
        }
        #endregion
        private Panel _end;
        /// <summary>
        /// 获取菜单底部对象。
        /// </summary>
        /// <remarks>获取菜单底部对象。</remarks>
        /// <value>System.Web.UI.WebControls.Panel</value>
        [Browsable(false)]
        public Panel End
        {
            get
            {
                return _end;
            }
        }
        /// <summary>
        /// 获取或设置是否显示底部对象。
        /// </summary>
        /// <remarks>获取或设置是否显示底部对象。</remarks>
        /// <value>bool</value>
        [DefaultValue(true)]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Behavior")]
        [SRDescription("ShowEnd", typeof(Properties.Resources))]
        public bool ShowEnd
        {
            get
            {
                object o = this.ViewState["ShowEnd"];
                if (o == null) return true;
                return (bool)o;
            }
            set
            {
                this.ViewState["ShowEnd"] = value;
            }
        }

        /// <summary>
        /// </summary>
        protected virtual void CreateControlHierarchy()
        {
            foreach (BackgroundCenterLeftMenuTitleControl item in this.Titles)
            {
                Controls.Add(item);
            }
            //
            this._end = new Panel();
            this._end.ID = "End";
            Controls.Add(_end);
        }
        #region MenuAction
        /// <summary>
        /// 标题动作事件。
        /// </summary>
        /// <remarks>标题动作事件。</remarks>
        /// <param name="e"></param>
        protected virtual void OnAction(BackgroundCenterLeftMenuControlActionEventArgs e)
        {
            CenterLeftMenuActionEventHandler handler = (CenterLeftMenuActionEventHandler)Events[_actionObject];
            if (handler != null)
            {
                handler(this, e);
            }
        }
        /// <summary>
        /// 标题动作事件。
        /// </summary>
        /// <remarks>标题动作事件。</remarks>
        [Browsable(true)]
        public event CenterLeftMenuActionEventHandler Action
        {
            add
            {
                Events.AddHandler(_actionObject, value);
            }
            remove
            {
                Events.RemoveHandler(_actionObject, value);
            }
        }
        private static readonly object _actionObject = new object();
        #endregion
        #region Data
        /// <summary>
        /// </summary>
        /// <param name="data"></param>
        protected override void PerformDataBinding(System.Collections.IEnumerable data)
        {
            if (data == null || string.IsNullOrEmpty(this.DataSourceID))
                return;
            this.Titles.Clear();
            //键值
            string _DataTextField = this.DataTextField;
            string _DataCommandArgumentField = this.DataCommandArgumentField;
            string _DataCommandNameField = this.DataCommandNameField;
            string _DataToolTipField = this.DataToolTipField;
            string _DataParentField = this.DataParentField;
            string _DataValueField = this.DataValueField;
            int parentID = 0;
            int primaryKey = 0;
            //
            foreach (object dataItem in data)
            {
                //获取父级ID。
                if (!string.IsNullOrEmpty(_DataParentField))
                {
                    parentID = (int)DataBinder.GetPropertyValue(dataItem, _DataParentField);
                    //读取父级标题。
                    if (parentID.Equals(0))
                    {
                        //获取主键ID。
                        if (!string.IsNullOrEmpty(_DataValueField))
                        {
                            primaryKey = (int)DataBinder.GetPropertyValue(dataItem, _DataValueField);
                        }
                        BackgroundCenterLeftMenuTitleControl t = new BackgroundCenterLeftMenuTitleControl();
                        if (!string.IsNullOrEmpty(_DataTextField))
                        {
                            t.Text = DataBinder.GetPropertyValue(dataItem, _DataTextField, null);
                        }
                        //
                        if (!string.IsNullOrEmpty(_DataCommandArgumentField))
                        {
                            t.CommandArgument = DataBinder.GetPropertyValue(dataItem, _DataCommandArgumentField, null);
                        }
                        //
                        if (!string.IsNullOrEmpty(_DataCommandNameField))
                        {
                            t.CommandName = DataBinder.GetPropertyValue(dataItem, _DataCommandNameField, null);
                        }
                        //
                        if (!string.IsNullOrEmpty(_DataToolTipField))
                        {
                            t.ToolTip = DataBinder.GetPropertyValue(dataItem, _DataToolTipField, null);
                        }
                        this.Titles.Add(t);
                        foreach (object cdataItem in data)
                        {
                            //获取父级ID。
                            parentID = (int)DataBinder.GetPropertyValue(cdataItem, _DataParentField);
                            if (parentID.Equals(primaryKey))
                            {
                                BackgroundCenterLeftMenuButtonControl b = new BackgroundCenterLeftMenuButtonControl();
                                if (!string.IsNullOrEmpty(_DataTextField))
                                {
                                    b.Text = DataBinder.GetPropertyValue(cdataItem, _DataTextField, null);
                                }
                                //
                                if (!string.IsNullOrEmpty(_DataCommandArgumentField))
                                {
                                    b.CommandArgument = DataBinder.GetPropertyValue(cdataItem, _DataCommandArgumentField, null);
                                }
                                //
                                if (!string.IsNullOrEmpty(_DataCommandNameField))
                                {
                                    b.CommandName = DataBinder.GetPropertyValue(cdataItem, _DataCommandNameField, null);
                                }
                                //
                                if (!string.IsNullOrEmpty(_DataToolTipField))
                                {
                                    b.ToolTip = DataBinder.GetPropertyValue(cdataItem, _DataToolTipField, null);
                                }
                                t.Buttons.Add(b);
                            }
                        }
                    }
                }
            }
            if (!this.DesignMode)
            {
                base.ChildControlsCreated = false;
                this.EnsureChildControls();
            }
        }
        /// <summary>
        /// 获取或设置按钮指令例。
        /// </summary>
        /// <remarks>获取或设置按钮指令例。</remarks>
        /// <value>string</value>
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [TypeConverter(typeof(DataFieldConverter))]
        [Bindable(false)]
        [SRDescription("DataCommandNameField", typeof(Properties.Resources))]
        public virtual string DataCommandNameField
        {
            get
            {
                object o = this.ViewState["DataCommandNameField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataCommandNameField"] = value;
            }
        }
        /// <summary>
        /// 获取或设置按钮附加指令例。
        /// </summary>
        /// <remarks>获取或设置按钮附加指令例。</remarks>
        /// <value>string</value>
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [TypeConverter(typeof(DataFieldConverter))]
        [Bindable(false)]
        [SRDescription("DataCommandArgumentField", typeof(Properties.Resources))]
        public virtual string DataCommandArgumentField
        {
            get
            {
                object o = this.ViewState["DataCommandArgumentField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataCommandArgumentField"] = value;
            }
        }
        /// <summary>
        /// 获取或设置按钮名称例。
        /// </summary>
        /// <remarks>获取或设置按钮名称例。</remarks>
        /// <value>string</value>
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [TypeConverter(typeof(DataFieldConverter))]
        [Bindable(false)]
        [SRDescription("DataTextField", typeof(Properties.Resources))]
        public virtual string DataTextField
        {
            get
            {
                object o = this.ViewState["DataTextField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataTextField"] = value;
            }
        }
        /// <summary>
        /// 获取或设置当鼠标指针悬停在按钮上时显示的文本例。
        /// </summary>
        /// <remarks>获取或设置当鼠标指针悬停在按钮上时显示的文本例。</remarks>
        /// <value>string</value>
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [TypeConverter(typeof(DataFieldConverter))]
        [Bindable(false)]
        [SRDescription("DataToolTipField", typeof(Properties.Resources))]
        public virtual string DataToolTipField
        {
            get
            {
                object o = this.ViewState["DataFlagField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataFlagField"] = value;
            }
        }
        /// <summary>
        /// 获取或设置按钮父级例。
        /// </summary>
        /// <remarks>获取或设置按钮父级例。</remarks>
        /// <value>string</value>
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [TypeConverter(typeof(DataFieldConverter))]
        [Bindable(false)]
        [SRDescription("DataParentField", typeof(Properties.Resources))]
        public virtual string DataParentField
        {
            get
            {
                object o = this.ViewState["DataParentField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataParentField"] = value;
            }
        }
        /// <summary>
        /// 获取或设置按钮主键例。
        /// </summary>
        /// <remarks>获取或设置按钮主键例。</remarks>
        /// <value>string</value>
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [TypeConverter(typeof(DataFieldConverter))]
        [Bindable(false)]
        [SRDescription("DataValueField", typeof(Properties.Resources))]
        public string DataValueField
        {
            get
            {
                object o = this.ViewState["DataValueField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataValueField"] = value;
            }
        }
        /// <summary>
        /// 获取或设置控件的 ID，数据绑定控件从该控件中检索其数据项列表。
        /// </summary>
        /// <remarks>获取或设置控件的 ID，数据绑定控件从该控件中检索其数据项列表。</remarks>
        /// <value>string</value>
        [RefreshProperties(RefreshProperties.Repaint)]
        [IDReferenceProperty(typeof(DataSourceControl))]
        [TypeConverter(typeof(DataSourceIDConverter))]
        [DefaultValue("")]
        [Bindable(false)]
        [Browsable(true)]
        [SRDescription("DataSourceID", typeof(Properties.Resources))]
        public override string DataSourceID
        {
            get
            {
                return base.DataSourceID;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    this.DataTextField = string.Empty;
                    this.DataCommandArgumentField = string.Empty;
                    this.DataCommandNameField = string.Empty;
                    this.DataMember = string.Empty;
                    this.DataValueField = string.Empty;
                    this.DataParentField = string.Empty;
                    this.DataToolTipField = string.Empty;
                }
                if (base.DataSourceID != value)
                {
                    base.DataSourceID = value;
                    if (this.Initialized)
                    {
                        this.OnDataPropertyChanged();
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置对象，数据绑定控件从该对象中检索其数据项列表。
        /// </summary>
        /// <remarks>获取或设置对象，数据绑定控件从该对象中检索其数据项列表。</remarks>
        /// <value>object</value>
        [Browsable(false)]
        [Bindable(true)]
        [DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                if (base.DataSource != value)
                {
                    base.DataSource = value;
                    if (this.Initialized)
                    {
                        this.OnDataPropertyChanged();
                    }
                }
            }
        }
        /// <summary>
        /// 当数据源包含多个不同的数据项列表时，获取或设置数据绑定控件绑定到的数据列表的名称。
        /// </summary>
        /// <remarks>当数据源包含多个不同的数据项列表时，获取或设置数据绑定控件绑定到的数据列表的名称。</remarks>
        /// <value>string</value>
        [RefreshProperties(RefreshProperties.All)]
        [TypeConverter(typeof(DataMemberConverter))]
        [DefaultValue("")]
        [Bindable(false)]
        [Browsable(true)]
        [SRDescription("DataMember", typeof(Properties.Resources))]
        public override string DataMember
        {
            get
            {
                return base.DataMember;
            }
            set
            {
                if (base.DataMember != value)
                {
                    base.DataMember = value;
                    if (this.Initialized)
                    {
                        this.OnDataPropertyChanged();
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置尾部高度。
        /// </summary>
        /// <remarks>获取或设置尾部高度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Bindable(true)]
        [DefaultValue(typeof(Unit), "6px")]
        [SRDescription("EndHeight", typeof(Properties.Resources))]
        [Browsable(true)]
        [Category("Layout")]
        [TypeConverter(typeof(UnitConverter))]
        public Unit EndHeight
        {
            get
            {
                object o = this.ViewState["EndHeight"];
                if (o == null) return Unit.Pixel(6);
                return (Unit)o;
            }
            set
            {
                this.ViewState["EndHeight"] = value;
            }
        }
        #endregion
        /// <summary>
        /// 获取或设置自动加载控件对象。
        /// </summary>
        /// <remarks>获取或设置自动加载控件对象。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [TypeConverter(typeof(ControlIDConverter))]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Behavior")]
        [SRDescription("AutomaticLoadControlID", typeof(Properties.Resources))]
        public string AutomaticLoadControlID
        {
            get
            {
                object o = this.ViewState["AutomaticLoadControlID"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["AutomaticLoadControlID"] = value;
            }
        }
        /// <summary>
        /// 获取或设置是否自动加载菜单对应的控件。
        /// </summary>
        /// <remarks>获取或设置是否自动加载菜单对应的控件。</remarks>
        /// <value>bool</value>
        [DefaultValue(false)]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Behavior")]
        [SRDescription("AutomaticLoadControl", typeof(Properties.Resources))]
        public bool AutomaticLoadControl
        {
            get
            {
                object o = this.ViewState["AutomaticLoadControl"];
                if (o == null) return false;
                return (bool)o;
            }
            set
            {
                this.ViewState["AutomaticLoadControl"] = value;
            }
        }

        /// <summary>
        /// 获取或设置标题文字宽度，在文字有溢出时设置。
        /// </summary>
        /// <remarks>获取或设置标题文字宽度，在文字有溢出时设置。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "")]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Layout")]
        [SRDescription("Manage_CenterLeftMenu_TitleTextWidth", typeof(Properties.Resources))]
        [TypeConverter(typeof(UnitConverter))]
        public virtual Unit TitleTextWidth
        {
            get
            {
                object o = this.ViewState["TitleTextWidth"];
                if (o == null)
                {
                    return Unit.Empty;
                }
                else
                {
                    return (Unit)o;
                }
            }
            set
            {
                this.ViewState["TitleTextWidth"] = value;
            }
        }

        /// <summary>
        /// 获取或设置菜单标题文字上边距离。
        /// </summary>
        /// <remarks>获取或设置菜单标题文字上边距离。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "5px")]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Layout")]
        [SRDescription("Manage_CenterLeftMenu_TitleTextMarginTop", typeof(Properties.Resources))]
        [TypeConverter(typeof(UnitConverter))]
        public Unit TitleTextMarginTop
        {
            get
            {
                object o = this.ViewState["TitleTextMarginTop"];
                if (o == null) return Unit.Pixel(5);
                return (Unit)o;
            }
            set
            {
                if (value.IsEmpty) value = Unit.Pixel(5);
                this.ViewState["TitleTextMarginTop"] = value;
            }
        }

        /// <summary>
        /// 不折叠展开的标题重置默认状态。
        /// </summary>
        /// <remarks>不折叠展开的标题重置默认状态。</remarks>
        public void ResetDefault()
        {
            ResetDefault(false);
        }
        /// <summary>
        /// 重置默认状态。
        /// </summary>
        /// <remarks>重置默认状态。</remarks>
        /// <param name="isCollapseTitle">设置是否折叠标题。</param>
        public void ResetDefault(bool isCollapseTitle)
        {
            foreach (BackgroundCenterLeftMenuTitleControl item in this.Titles)
            {
                if (isCollapseTitle && item.IsExpanded)
                {
                    item.IsExpanded = false;
                }
                foreach (BackgroundCenterLeftMenuButtonControl citem in item.Buttons)
                {
                    if (citem.IsSelected)
                    {
                        citem.IsSelected = false;
                    }
                }
            }
        }
        /// <summary>
        /// 是否清除状态设图数据。
        /// </summary>
        /// <remarks>是否清除状态设图数据。</remarks>
        internal bool _isClearState = true;
        /// <summary>
        /// 折叠状态列表。
        /// </summary>
        /// <remarks>折叠状态列表。</remarks>
        internal Dictionary<int, bool> _stateDataExpandedList = new Dictionary<int, bool>();
        /// <summary>
        /// 折叠状态列表。
        /// </summary>
        /// <remarks>折叠状态列表。</remarks>
        internal Dictionary<int, bool> _stateDataSelectedList = new Dictionary<int, bool>();
    }
}